﻿Imports Mysoft.Map.Data
Imports System.Xml

'费项管理器
Public Class CostManager

    '保存集团费项
    Public Shared Function SaveBZItem(ByVal DataXml As String, ByVal UserDataXml As String) As String
        Dim xmlDOM As New XmlDocument
        Dim strKeyValue, strIsBudgetItem, strIsEndBudgetItem, strItemCode, strItemName, strParentCode, strIsJianAnCost As String
        Dim dtTemp As DataTable
        Dim OID, strSQL As String

        xmlDOM.LoadXml(DataXml)

        strKeyValue = xmlDOM.DocumentElement.Attributes.GetNamedItem("keyvalue").InnerText
        strIsBudgetItem = xmlDOM.DocumentElement.SelectSingleNode("IsBudgetItem").InnerText
        strIsEndBudgetItem = xmlDOM.DocumentElement.SelectSingleNode("IsEndBudgetItem").InnerText
        strItemCode = xmlDOM.DocumentElement.SelectSingleNode("ItemCode").InnerText
        strItemName = xmlDOM.DocumentElement.SelectSingleNode("ItemName").InnerText
        strParentCode = xmlDOM.DocumentElement.SelectSingleNode("ParentCode").InnerText
        strIsJianAnCost = xmlDOM.DocumentElement.SelectSingleNode("IsJianAnCost").InnerText

        If strKeyValue <> "" Then
            strSQL = "SELECT ItemCode,ItemName,IsBudgetItem,IsEndBudgetItem,IfEnd,Level,IsJianAnCost FROM P_BZItem WHERE BZItemGUID='" & strKeyValue & "'"
            dtTemp = MyDB.GetDataTable(strSQL)
        End If

        OID = MyDB.SaveXml(DataXml)

        If OID <> "" Then
            strSQL = ""
            If strKeyValue = "" Then
                '新增
                If strIsBudgetItem = "1" Then
                    strSQL = "UPDATE p_BZItem SET IfEnd=0,IsEndBudgetItem=0 WHERE ItemCode='" & strParentCode & "'"
                Else
                    strSQL = "UPDATE p_BZItem Set IfEnd=0 WHERE ItemCode='" & strParentCode & "'"
                End If
            Else
                '修改
                If strIsBudgetItem <> dtTemp.Rows(0)("IsEndBudgetItem").ToString() Then
                    If strIsBudgetItem = "1" Then
                        '由非管控改为了管控费项
                        strSQL = "UPDATE p_BZItem SET IsEndBudgetItem=0 WHERE ItemCode='" & strParentCode & "';"
                    Else
                        '由管控费项改为了非管控费项
                        strSQL = "UPDATE p_BZItem SET IsEndBudgetItem=1 WHERE ItemCode='" & strParentCode & "';"
                    End If
                End If

                If dtTemp.Rows(0)("IfEnd").ToString() = "0" Then
                    Dim strItemCodeOld, strItemNameOld As String
                    strItemCodeOld = dtTemp.Rows(0)("ItemCode").ToString()
                    strItemNameOld = dtTemp.Rows(0)("ItemName").ToString()

                    If dtTemp.Rows(0)("Level") = 2 Then
                        If dtTemp.Rows(0)("IsJianAnCost").ToString <> strIsJianAnCost Then
                            strSQL &= "UPDATE p_BZItem Set IsJianAnCost=" & strIsJianAnCost & " WHERE Charindex('" & strItemCodeOld & ".', ItemCode)=1;"
                        End If
                    End If

                    If strItemName <> strItemNameOld Then
                        strSQL &= "UPDATE p_BZItem SET ItemName=Replace(ItemName,'" & strItemNameOld & "','" & strItemName & "') WHERE Charindex('" & strItemNameOld & "-',ItemName)=1;"
                    End If

                    If strItemCode <> strItemCodeOld Then
                        strSQL &= "UPDATE p_BZItem SET ParentCode=Replace(ParentCode,'" & strItemCodeOld & "','" & strItemCode & "'),ItemCode=Replace(ItemCode,'" & strItemCodeOld & "','" & strItemCode & "') WHERE Charindex('" & strItemCodeOld & ".',ItemCode)=1;"
                    End If
                End If
            End If

            If strSQL <> "" Then
                If MyDB.ExecSQL(strSQL) = -1 Then
                    Return "<xml result=""false"" errormessage=""操作失败，请关闭重试！""></xml>"
                End If
            End If

            Return "<xml result=""true"" keyvalue=""" & OID & """></xml>"
        Else
            Return "<xml result=""false"" errormessage=""运行时异常，请与管理员联系！""></xml>"
        End If

    End Function

    '保存费项
    Public Shared Function SaveCost(ByVal DataXml As String, ByVal UserDataXml As String) As String
        Dim xmlDOM As New XmlDocument
        Dim strKeyValue, strCostCode, strCostName, strParentCode, strBUGUID, strIsJianAnCost As String
        Dim dtTemp As DataTable
        Dim OID, strSQL As String
        Dim strTableName, strKeyName, strWhere As String

        xmlDOM.LoadXml(DataXml)

        strTableName = xmlDOM.DocumentElement.Name
        strKeyName = xmlDOM.DocumentElement.Attributes.GetNamedItem("keyname").InnerText()
        strKeyValue = xmlDOM.DocumentElement.Attributes.GetNamedItem("keyvalue").InnerText
        strCostCode = xmlDOM.DocumentElement.SelectSingleNode("CostCode").InnerText
        strCostName = xmlDOM.DocumentElement.SelectSingleNode("CostName").InnerText
        strParentCode = xmlDOM.DocumentElement.SelectSingleNode("ParentCode").InnerText
        strBUGUID = xmlDOM.DocumentElement.SelectSingleNode("BUGUID").InnerText
        strIsJianAnCost = xmlDOM.DocumentElement.SelectSingleNode("IsJianAnCost").InnerText

        If strKeyValue <> "" Then
            strSQL = "SELECT CostLevel,CostCode,CostName,IfEndCost FROM " & strTableName & " WHERE " & strKeyName & "='" & strKeyValue & "'"
            dtTemp = MyDB.GetDataTable(strSQL)
        End If

        OID = MyDB.SaveXml(DataXml)

        If OID <> "" Then
            strSQL = ""
            If strTableName <> "cb_Cost" Then
                strWhere = " AND BUGUID='" & strBUGUID & "'"
            Else
                strWhere = " AND ProjectCode='" & xmlDOM.DocumentElement.SelectSingleNode("ProjectCode").InnerText & "'"
            End If
            If strKeyValue = "" Then
                '新增
                strSQL = "UPDATE " & strTableName & " SET IfEndCost=0 WHERE CostCode='" & strParentCode & "'" & strWhere
            Else
                '修改
                Dim strCostCodeOld, strCostNameOld As String
                strCostCodeOld = dtTemp.Rows(0)("CostCode").ToString()
                strCostNameOld = dtTemp.Rows(0)("CostName").ToString()
                If dtTemp.Rows(0)("IfEndCost").ToString() = "0" Then

                    If dtTemp.Rows(0)("CostLevel") = 2 Then
                        If dtTemp.Rows(0)("IsJianAnCost").ToString <> strIsJianAnCost Then
                            strSQL &= "UPDATE " & strTableName & " Set IsJianAnCost=" & strIsJianAnCost & " WHERE Charindex('" & strCostCodeOld & ".', CostCode)=1;"
                        End If
                    End If

                    If strCostName <> strCostNameOld Then
                        strSQL &= "UPDATE " & strTableName & " SET ItemName=Replace(ItemName,'" & strCostNameOld & "','" & strCostName & "') WHERE ParentCode='" & strCostNameOld & "'" & strWhere & ";"
                    End If
                    If strCostCode <> strCostCodeOld Then
                        strSQL &= "UPDATE " & strTableName & " SET ParentCode='" & strCostCode & "',ItemCode=Replace(ItemCode,'" & strCostCodeOld & "','" & strCostCode & "') WHERE ParentCode='" & strCostCodeOld & "'" & strWhere & ";"
                    End If
                End If
            End If

            If strSQL <> "" Then
                If MyDB.ExecSQL(strSQL) = -1 Then
                    Return "<xml result=""false"" errormessage=""操作失败，请关闭重试！""></xml>"
                End If
            End If

            Return "<xml result=""true"" keyvalue=""" & OID & """></xml>"
        Else
            Return "<xml result=""false"" errormessage=""运行时异常，请与管理员联系！""></xml>"
        End If

    End Function

    '获得过滤条件
    Public Function GetFilter(ByVal ObjValue As String, ByVal CostType As CostTypeEnum, ByVal IsPopedomFilter As Boolean)
        Dim strFilter As String = ""
        Select Case CostType
            Case CostTypeEnum.ProjCost
                strFilter += " ProjectCode='" & ObjValue & "'"

            Case CostTypeEnum.CompanyCost
                strFilter += " BUGUID='" & ObjValue & "' AND ItemType=2"

            Case CostTypeEnum.DeptCost
                strFilter += " BUGUID='" & ObjValue & "' AND ItemType=3"

            Case CostTypeEnum.AllCompanyCost
                strFilter += "  BUGUID='" & ObjValue & "' AND ProjectType='公司'"

        End Select

        If IsPopedomFilter Then
            strFilter += " AND "
        End If

        Return strFilter
    End Function

    '获得成本费项过滤子表
    Public Function GetFilterSec(ByVal strBUGUID As String, ByVal strUserGUID As String)
        Dim strSql As String
        If Mysoft.Map.Application.Security.User.IsAdmin(strUserGUID) Then
            'strSql = "SELECT BUCode FROM myBusinessUnit WHERE BUGUID='" & strBUGUID & "'"
            'Dim strBUCode As String = MyDB.GetDataItemString(strSql)
            'strSql = "select CostCode from cb_Cost where ProjectCode='" & strBUCode & "' and cb_Cost.CostType = '项目成本'"
            strSql = "select a.CostCode,b.ProjGUID from cb_Cost a INNER JOIN p_Project b ON a.ProjectCode = b.ProjCode where a.CostType = '项目成本'"
        Else
            strSql = "select DISTINCT cb_Cost.CostCode,b.ProjGUID from cb_CostStationRights" & _
                    " left join myStationUser on cb_CostStationRights.StationGUID=myStationUser.StationGUID" & _
                    " left join cb_Cost on cb_CostStationRights.CostGUID=cb_Cost.CostGUID INNER JOIN p_Project b ON cb_Cost.ProjectCode = b.ProjCode" & _
                    " where cb_CostStationRights.BUGUID='" & strBUGUID & "' and myStationUser.UserGUID='" & strUserGUID & "'" & _
                    " group by cb_Cost.CostCode,b.ProjGUID"
        End If

        Return strSql
    End Function

    '获得部门费项过滤子表
    Public Function GetDeptCostFilterSec(ByVal strBUGUID As String, ByVal strUserGUID As String)
        Dim strSql As String
        If Mysoft.Map.Application.Security.User.IsAdmin(strUserGUID) Then
            strSql = "SELECT BUCode FROM myBusinessUnit WHERE BUGUID='" & strBUGUID & "'"
            Dim strBUCode As String = MyDB.GetDataItemString(strSql)
            strSql = "select CostCode from ys_DeptCost where BUGUID='" & strBUGUID & "' and IsEndBudgetItem=1"
        Else
            '说明：内层select出最末级费项，外层select显示出上级费项
            strSql = "(select aa.CostCode from ys_DeptCost aa," & _
                    "(select c.CostCode,c.Buguid from cb_CostStationRights" & _
                    " left join myStationUser on cb_CostStationRights.StationGUID=myStationUser.StationGUID" & _
                    " left join ys_DeptCost c on cb_CostStationRights.CostGUID=c.DeptCostGUID" & _
                    " where cb_CostStationRights.BUGUID='" & strBUGUID & "' and myStationUser.UserGUID='" & strUserGUID & "') bb" & _
                    " where aa.Buguid=bb.Buguid and charindex(aa.CostCode+'.',bb.CostCode+'.')=1  " & _
                    " Group by aa.CostCode )"
        End If

        Return strSql
    End Function


    '获得公司库存部门费项过滤子表
    Public Function GetStockCostFilterSec(ByVal strBUGUID As String, ByVal strUserGUID As String)
        Dim strSql As String
        If Mysoft.Map.Application.Security.User.IsAdmin(strUserGUID) Then
            strSql = "SELECT BUCode FROM myBusinessUnit WHERE BUGUID='" & strBUGUID & "'"
            Dim strBUCode As String = MyDB.GetDataItemString(strSql)
            strSql = "select CostCode from cb_StockCost where BUGUID='" & strBUGUID & "'"
        Else
            '说明：内层select出最末级费项，外层select显示出上级费项
            strSql = "(select aa.CostCode from cb_StockCost aa," & _
                    "(select c.CostCode,c.Buguid from cb_CostStationRights" & _
                    " left join myStationUser on cb_CostStationRights.StationGUID=myStationUser.StationGUID" & _
                    " left join cb_StockCost c on cb_CostStationRights.CostGUID=c.StockCostGUID" & _
                    " where cb_CostStationRights.BUGUID='" & strBUGUID & "' and myStationUser.UserGUID='" & strUserGUID & "') bb" & _
                    " where aa.Buguid=bb.Buguid and charindex(aa.CostCode+'.',bb.CostCode+'.')=1  " & _
                    " Group by aa.CostCode )"
        End If

        Return strSql
    End Function

    '更新指定费项的合约规划
    Public Function UpdateLayoutSpare(ByVal CostCode As String, ByVal ProjectCode As String) As Boolean
        Dim strSql As String
        strSql = "usp_cb_UpdateCostBudget '" & CostCode & "','" & ProjectCode & "'"
        MyDB.ExecSQL(strSql)

        Return True
    End Function

    '重算动态成本
    Public Function CalcDTCost(ByVal ProjectCode As String) As Boolean

    End Function

    '更新TargetCost,同时更新其它字段
    Public Function UpdateTargetCost(ByVal ProjectCode As String, ByVal Edition As String) As Boolean
        Dim strSql As String
        strSql = "Exec usp_cb_UpdateTarger '" & ProjectCode & "','" & Edition & "'"
        MyDB.ExecSQL(strSql)

        Return True
    End Function

    '数据整理
    '如果CostType = CostTypeEnum.ProjCost,则ObjValue为项目Code,其它情况均为公司GUID
    '如果CostType = CostTypeEnum.None 或 CostType = CostTypeEnum.GroupCost,则不做任何处理,直接返回false
    Public Function RecalcCost(ByVal ObjValue As String, ByVal CostType As CostTypeEnum) As Boolean

    End Function

    '获得指定类别的费项的最大级数
    Public Function GetCostLevel(ByVal ObjValue As String, ByVal CostType As CostTypeEnum, Optional ByVal IsBudgetItem As Integer = 1) As Integer
        Dim strSql As String
        Select Case CostType
            Case CostTypeEnum.ProjCost
                strSql = "SELECT max(CostLevel) FROM cb_Cost WHERE ProjectCode='" & ObjValue & "' AND IsBudgetItem=" & IsBudgetItem

            Case CostTypeEnum.DeptCost
                strSql = "SELECT max(CostLevel) FROM ys_DeptCost WHERE BUGUID='" & ObjValue & "' AND IsBudgetItem=" & IsBudgetItem

            Case CostTypeEnum.CompanyCost
                strSql = "SELECT max(CostLevel) FROM Cb_StockCost WHERE BUGUID='" & ObjValue & "' AND IsBudgetItem=" & IsBudgetItem

            Case CostTypeEnum.AllCompanyCost
                strSql = "SELECT max(CostLevel) FROM vcb_CostAll WHERE BUGUID='" & ObjValue & "' AND ProjectType='公司' AND IsBudgetItem=" & IsBudgetItem

            Case CostTypeEnum.GroupCost
                strSql = "SELECT max(Level) FROM p_BZItem WHERE IsBudgetItem=" & IsBudgetItem

        End Select

        Return MyDB.GetDataItemInt(strSql)

    End Function

    '获得指定类型的费项展开级数的HTML代码
    Public Function GetCostLevelHtml(ByVal ObjValue As String, ByVal CostType As CostTypeEnum, Optional ByVal objControl As String = "") As String
        Dim MaxLevel As Integer = GetCostLevel(ObjValue, CostType)
        Dim strHtml As String = ""
        Dim i As Integer

        If objControl <> "" Then
            If objControl.ToLower().IndexOf("appgride") < 0 Then
                objControl = objControl & ".showLevel("
            Else
                objControl = objControl & ".ShowLevel("
            End If
        Else
            objControl = "showLevel("
        End If


        For i = 1 To MaxLevel
            strHtml &= "<a href='javascript:" & objControl & i & ")' style='color:white;text-decoration: none' >&nbsp;" & i & "&nbsp;</a>"
        Next

        Return strHtml
    End Function

    '提交审批、审批通过、审批驳回
    '动态成本=目标成本+调整成本
    '动态成本=合约规划+预估变更+规划余量
    Public Shared Function ApproveAdjust(ByVal DataXml As String, ByVal UserDataXml As String) As String
        Dim xmlDOM As New XmlDocument
        Dim xmlDOM2 As New XmlDocument
        Dim xmlDOMDataXml As New XmlDocument
        Dim myAction As String = ""
        Dim myTitle As String = ""
        Dim xmlTempNode As XmlNode
        Dim strGuidValue As String = ""
        Dim strProjectCode As String = ""
        Dim strCostGUIDImport As String = ""
        Dim strCostGUIDExport As String = ""

        Dim strCostCodeImport As String = ""
        Dim strCostCodeExport As String = ""

        Dim strAdjustAmount As String = ""
        Dim strResult As String = ""
        Dim strSql As String = ""
        Dim strAppGridE As String = ""
        Dim sTable As DataTable
        Dim i As Int32 = 0

        xmlDOM.LoadXml(UserDataXml)
        xmlTempNode = xmlDOM.DocumentElement.SelectSingleNode("myTitle")
        myTitle = xmlTempNode.InnerText()
        xmlTempNode = xmlDOM.DocumentElement.SelectSingleNode("myAction")
        myAction = xmlTempNode.InnerText()
        xmlDOMDataXml.LoadXml(DataXml)
        xmlTempNode = xmlDOMDataXml.DocumentElement.SelectSingleNode("ProjGUID")
        strProjectCode = MyDB.GetDataItemString("select ProjCode from p_Project where ProjGUID='" + xmlTempNode.InnerText() + "'")
        If myTitle = "MBJZ" Then        '目标结转
            Select Case myAction
                Case "TjSave"    '工作流审批发起之前先保存数据
                    strResult = SaveAdjust(DataXml, "")
                    xmlDOM2.LoadXml(strResult)
                    strResult = xmlDOM2.DocumentElement.Attributes.GetNamedItem("result").InnerText()
                    If strResult = "true" Then
                        strGuidValue = xmlDOM2.DocumentElement.Attributes.GetNamedItem("keyvalue").InnerText()
                        'strSql = "update cb_Adjust set ApproveState='未审核' where AdjustGUID='" + strGuidValue + "'"
                    End If
                Case "TjApprove"    '提交审批
                    strResult = SaveAdjust(DataXml, "")
                    xmlDOM2.LoadXml(strResult)
                    strResult = xmlDOM2.DocumentElement.Attributes.GetNamedItem("result").InnerText()
                    If strResult = "true" Then
                        strGuidValue = xmlDOM2.DocumentElement.Attributes.GetNamedItem("keyvalue").InnerText()
                        strSql = "update cb_Adjust set ApproveState='审核中' where AdjustGUID='" + strGuidValue + "'"
                    End If
                Case "TgApprove"    '通过审批
                    strGuidValue = xmlDOMDataXml.DocumentElement.GetAttribute("keyvalue")
                    xmlTempNode = xmlDOMDataXml.DocumentElement.SelectSingleNode("AdjustAmount")
                    strAdjustAmount = xmlTempNode.InnerText()

                    xmlTempNode = xmlDOMDataXml.DocumentElement.SelectSingleNode("ImportCostGUID")
                    strCostGUIDImport = xmlTempNode.InnerText()
                    sTable = MyDB.GetDataTable("select CostCode from cb_cost where CostGUID='" + strCostGUIDImport + "'")
                    strCostCodeImport = sTable.Rows(0)("CostCode").ToString()

                    xmlTempNode = xmlDOMDataXml.DocumentElement.SelectSingleNode("ExportCostGUID")
                    strCostGUIDExport = xmlTempNode.InnerText()
                    sTable = MyDB.GetDataTable("select CostCode from cb_cost where CostGUID='" + strCostGUIDExport + "'")
                    strCostCodeExport = sTable.Rows(0)("CostCode").ToString()

                    '转出转入金额变更方式需要确认
                    If strAdjustAmount.IndexOf("-") < 0 Then
                        '转出
                        strSql = "update cb_cost set AdjustCost=AdjustCost-" + strAdjustAmount + ",LayoutSpare=LayoutSpare-" + strAdjustAmount + " where Charindex(CostCode+'.','" + strCostCodeExport + ".')=1 and charindex(ProjectCode+'.', '" + strProjectCode + ".')=1"
                        '转入
                        strSql += ";update cb_cost set AdjustCost=AdjustCost+" + strAdjustAmount + ",LayoutSpare=LayoutSpare+" + strAdjustAmount + " where Charindex(CostCode+'.','" + strCostCodeImport + ".')=1 and charindex(ProjectCode+'.', '" + strProjectCode + ".')=1"
                    Else
                        '转出
                        strSql = "update cb_cost set AdjustCost=AdjustCost+abs(" + strAdjustAmount + "),LayoutSpare=LayoutSpare+abs(" + strAdjustAmount + ") where Charindex(CostCode+'.','" + strCostCodeExport + ".')=1 and charindex(ProjectCode+'.', '" + strProjectCode + ".')=1"
                        '转入
                        strSql += ";update cb_cost set AdjustCost=AdjustCost-abs(" + strAdjustAmount + "),LayoutSpare=LayoutSpare-abs(" + strAdjustAmount + ") where Charindex(CostCode+'.','" + strCostCodeImport + ".')=1 and charindex(ProjectCode+'.', '" + strProjectCode + ".')=1"
                    End If
                    strSql += ";update cb_Adjust set ApproveState='已审核' where AdjustGUID='" + strGuidValue + "'"              '更改变更单状态
                Case "BhSpzApprove"     '驳回审批中的
                    strGuidValue = xmlDOMDataXml.DocumentElement.GetAttribute("keyvalue")
                    strSql += "update cb_Adjust set ApproveState='未审核' where AdjustGUID='" + strGuidValue + "'"              '更改变更单状态
                Case "BhYspApprove"     '驳回已审批的
                    strGuidValue = xmlDOMDataXml.DocumentElement.GetAttribute("keyvalue")
                    xmlTempNode = xmlDOMDataXml.DocumentElement.SelectSingleNode("AdjustAmount")
                    strAdjustAmount = xmlTempNode.InnerText()
                    xmlTempNode = xmlDOMDataXml.DocumentElement.SelectSingleNode("ImportCostGUID")
                    strCostGUIDImport = xmlTempNode.InnerText()
                    sTable = MyDB.GetDataTable("select CostCode from cb_cost where CostGUID='" + strCostGUIDImport + "'")
                    strCostCodeImport = sTable.Rows(0)("CostCode").ToString()

                    xmlTempNode = xmlDOMDataXml.DocumentElement.SelectSingleNode("ExportCostGUID")
                    strCostGUIDExport = xmlTempNode.InnerText()
                    sTable = MyDB.GetDataTable("select CostCode from cb_cost where CostGUID='" + strCostGUIDExport + "'")
                    strCostCodeExport = sTable.Rows(0)("CostCode").ToString()
                    '转出转入金额变更方式需要确认
                    If strAdjustAmount.IndexOf("-") < 0 Then
                        '转出
                        strSql = "update cb_cost set AdjustCost=AdjustCost+" + strAdjustAmount + ",LayoutSpare=LayoutSpare+" + strAdjustAmount + " where Charindex(CostCode+'.','" + strCostCodeExport + ".')=1 and charindex(ProjectCode+'.', '" + strProjectCode + ".')=1"
                        '转入
                        strSql += ";update cb_cost set AdjustCost=AdjustCost-" + strAdjustAmount + ",LayoutSpare=LayoutSpare-" + strAdjustAmount + " where Charindex(CostCode+'.','" + strCostCodeImport + ".')=1 and charindex(ProjectCode+'.', '" + strProjectCode + ".')=1"
                    Else
                        '转出
                        strSql = "update cb_cost set AdjustCost=AdjustCost-abs(" + strAdjustAmount + "),LayoutSpare=LayoutSpare-abs(" + strAdjustAmount + ") where Charindex(CostCode+'.','" + strCostCodeExport + ".')=1 and charindex(ProjectCode+'.', '" + strProjectCode + ".')=1"
                        '转入
                        strSql += ";update cb_cost set AdjustCost=AdjustCost+abs(" + strAdjustAmount + "),LayoutSpare=LayoutSpare+abs(" + strAdjustAmount + ") where Charindex(CostCode+'.','" + strCostCodeImport + ".')=1 and charindex(ProjectCode+'.', '" + strProjectCode + ".')=1"
                    End If
                    strSql += ";update cb_Adjust set ApproveState='未审核' where AdjustGUID='" + strGuidValue + "'"              '更改变更单状态
            End Select
        End If
        If myTitle = "MBZJ" Then       '目标追加
            Select Case myAction
                Case "doSave"
                    xmlDOM2.LoadXml(SaveAdjustMBZJ(DataXml, ""))
                    strResult = xmlDOM2.DocumentElement.Attributes.GetNamedItem("result").InnerText()
                    If strResult = "true" Then
                        strGuidValue = xmlDOM2.DocumentElement.Attributes.GetNamedItem("keyvalue").InnerText()
                        strAppGridE = UserDataXml.Replace("<myxml><myTitle>MBZJ</myTitle><myAction>doSave</myAction><userGridE>", "<?xml version=""1.0""?>").Replace("</userGridE></myxml>", "").Replace("AdjustGUID=""", "AdjustGUID=""" + strGuidValue)
                        Mysoft.CustomControl.AppGridEx.AppGridE.SaveXml(strAppGridE)
                        'strSql = "update cb_Adjust set ApproveState='未审核' where AdjustGUID='" + strGuidValue + "'"
                    End If
                Case "doSaveClose"
                    xmlDOM2.LoadXml(SaveAdjustMBZJ(DataXml, ""))
                    strResult = xmlDOM2.DocumentElement.Attributes.GetNamedItem("result").InnerText()
                    If strResult = "true" Then
                        strGuidValue = xmlDOM2.DocumentElement.Attributes.GetNamedItem("keyvalue").InnerText()
                        strAppGridE = UserDataXml.Replace("<myxml><myTitle>MBZJ</myTitle><myAction>doSaveClose</myAction><userGridE>", "<?xml version=""1.0""?>").Replace("</userGridE></myxml>", "").Replace("AdjustGUID=""", "AdjustGUID=""" + strGuidValue)
                        Mysoft.CustomControl.AppGridEx.AppGridE.SaveXml(strAppGridE)
                        'strSql = "update cb_Adjust set ApproveState='未审核' where AdjustGUID='" + strGuidValue + "'"
                    End If
                Case "TjApprove"
                    xmlDOM2.LoadXml(SaveAdjustMBZJ(DataXml, ""))
                    strResult = xmlDOM2.DocumentElement.Attributes.GetNamedItem("result").InnerText()
                    If strResult = "true" Then
                        strGuidValue = xmlDOM2.DocumentElement.Attributes.GetNamedItem("keyvalue").InnerText()
                        strAppGridE = UserDataXml.Replace("<myxml><myTitle>MBZJ</myTitle><myAction>TjApprove</myAction><userGridE>", "<?xml version=""1.0""?>").Replace("</userGridE></myxml>", "").Replace("AdjustGUID=""", "AdjustGUID=""" + strGuidValue)
                        Mysoft.CustomControl.AppGridEx.AppGridE.SaveXml(strAppGridE)
                        strSql = "UPDATE a SET a.PreAdjustLayoutSpare =b.LayoutSpare FROM cb_AdjustDtl a,cb_Cost b WHERE a.CostGUID=b.CostGUID AND a.AdjustGUID='" + strGuidValue + "';update cb_Adjust set ApproveState='审核中' where AdjustGUID='" + strGuidValue + "'"
                    End If
                Case "TgApprove"
                    strGuidValue = xmlDOMDataXml.DocumentElement.GetAttribute("keyvalue")
                    sTable = MyDB.GetDataTable("select b.CostCode,a.CostGUID,a.AdjustAmount from cb_adjustdtl a inner join cb_cost b on a.CostGUID=b.CostGUID where a.AdjustGUID='" + strGuidValue + "'")
                    strSql = ""
                    For i = 0 To sTable.Rows.Count - 1
                        If strSql = "" Then
                            If sTable.Rows(i)("AdjustAmount").ToString().IndexOf("-") < 0 Then
                                strSql = "update cb_cost set AdjustCost=AdjustCost+" + sTable.Rows(i)("AdjustAmount").ToString() + ",LayoutSpare=LayoutSpare+" + sTable.Rows(i)("AdjustAmount").ToString() + " where Charindex(CostCode+'.','" + sTable.Rows(i)("CostCode").ToString() + ".')=1 and charindex(ProjectCode+'.', '" + strProjectCode + ".')=1"
                            Else
                                strSql = "update cb_cost set AdjustCost=AdjustCost-abs(" + sTable.Rows(i)("AdjustAmount").ToString() + "),LayoutSpare=LayoutSpare-abs(" + sTable.Rows(i)("AdjustAmount").ToString() + ") where Charindex(CostCode+'.','" + sTable.Rows(i)("CostCode").ToString() + ".')=1 and charindex(ProjectCode+'.', '" + strProjectCode + ".')=1"
                            End If
                        Else
                            If sTable.Rows(i)("AdjustAmount").ToString().IndexOf("-") < 0 Then
                                strSql += ";update cb_cost set AdjustCost=AdjustCost+" + sTable.Rows(i)("AdjustAmount").ToString() + ",LayoutSpare=LayoutSpare+" + sTable.Rows(i)("AdjustAmount").ToString() + " where Charindex(CostCode+'.','" + sTable.Rows(i)("CostCode").ToString() + ".')=1 and charindex(ProjectCode+'.', '" + strProjectCode + ".')=1"
                            Else
                                strSql += ";update cb_cost set AdjustCost=AdjustCost-abs(" + sTable.Rows(i)("AdjustAmount").ToString() + "),LayoutSpare=LayoutSpare-abs(" + sTable.Rows(i)("AdjustAmount").ToString() + ") where Charindex(CostCode+'.','" + sTable.Rows(i)("CostCode").ToString() + ".')=1 and charindex(ProjectCode+'.', '" + strProjectCode + ".')=1"
                            End If
                        End If
                    Next
                    strSql += ";update cb_Adjust set ApproveState='已审核' where AdjustGUID='" + strGuidValue + "'"              '更改变更单状态
                Case "BhSpzApprove"     '驳回审批中的
                    strGuidValue = xmlDOMDataXml.DocumentElement.GetAttribute("keyvalue")
                    strSql += "update cb_Adjust set ApproveState='未审核' where AdjustGUID='" + strGuidValue + "'"              '更改变更单状态
                Case "BhYspApprove"     '驳回已审批的
                    strGuidValue = xmlDOMDataXml.DocumentElement.GetAttribute("keyvalue")
                    sTable = MyDB.GetDataTable("select b.CostCode,a.CostGUID,a.AdjustAmount from cb_adjustdtl a inner join cb_cost b on a.CostGUID=b.CostGUID where a.AdjustGUID='" + strGuidValue + "'")
                    strSql = ""
                    For i = 0 To sTable.Rows.Count - 1
                        If strSql = "" Then
                            If sTable.Rows(i)("AdjustAmount").ToString().IndexOf("-") < 0 Then
                                strSql = "update cb_cost set AdjustCost=AdjustCost-" + sTable.Rows(i)("AdjustAmount").ToString() + ",LayoutSpare=LayoutSpare-" + sTable.Rows(i)("AdjustAmount").ToString() + " where  Charindex(CostCode+'.','" + sTable.Rows(i)("CostCode").ToString() + ".')=1 and charindex(ProjectCode+'.', '" + strProjectCode + ".')=1"
                            Else
                                strSql = "update cb_cost set AdjustCost=AdjustCost+abs(" + sTable.Rows(i)("AdjustAmount").ToString() + "),LayoutSpare=LayoutSpare+abs(" + sTable.Rows(i)("AdjustAmount").ToString() + ") where  Charindex(CostCode+'.','" + sTable.Rows(i)("CostCode").ToString() + ".')=1 and charindex(ProjectCode+'.', '" + strProjectCode + ".')=1"
                            End If
                        Else
                            If sTable.Rows(i)("AdjustAmount").ToString().IndexOf("-") < 0 Then
                                strSql += ";update cb_cost set AdjustCost=AdjustCost-" + sTable.Rows(i)("AdjustAmount").ToString() + ",LayoutSpare=LayoutSpare-" + sTable.Rows(i)("AdjustAmount").ToString() + " where  Charindex(CostCode+'.','" + sTable.Rows(i)("CostCode").ToString() + ".')=1 and charindex(ProjectCode+'.', '" + strProjectCode + ".')=1"
                            Else
                                strSql += ";update cb_cost set AdjustCost=AdjustCost+abs(" + sTable.Rows(i)("AdjustAmount").ToString() + "),LayoutSpare=LayoutSpare+abs(" + sTable.Rows(i)("AdjustAmount").ToString() + ") where  Charindex(CostCode+'.','" + sTable.Rows(i)("CostCode").ToString() + ".')=1 and charindex(ProjectCode+'.', '" + strProjectCode + ".')=1"
                            End If
                        End If
                    Next
                    strSql += ";update cb_Adjust set ApproveState='未审核' where AdjustGUID='" + strGuidValue + "'"              '更改变更单状态
            End Select
        End If
        If strSql <> "" Then
            If MyDB.ExecSQL(strSql) < 0 Then
                Return "<xml result=""false"" errormessage=""运行时异常，请与管理员联系！""></xml>"
            Else
                Return "<xml result=""true"" keyvalue=""" & strGuidValue & """></xml>"
            End If
        Else
            Return "<xml result=""true"" keyvalue=""" & strGuidValue & """></xml>"
        End If
    End Function

    '保存目标追加
    Public Shared Function SaveAdjustMBZJ(ByVal DataXml As String, ByVal UserDataXml As String) As String
        Dim oid As String = MyDB.SaveXml(DataXml)
        If oid <> "" Then
            Return "<xml result=""true"" keyvalue=""" & oid & """></xml>"
        Else
            Return "<xml result=""false"" errormessage=""运行时异常，请与管理员联系！""></xml>"
        End If
    End Function

    '保存目标结转
    Public Shared Function SaveAdjust(ByVal DataXml As String, ByVal UserDataXml As String) As String
        Dim strAdjustAmount, strImportCostGUID, strExportCostGUID As String
        Dim xmlDOM As New XmlDocument
        Dim xmlTempNode As XmlNode
        Dim strKeyValue As String
        Dim strSql As String

        xmlDOM.LoadXml(DataXml)

        xmlTempNode = xmlDOM.DocumentElement.SelectSingleNode("AdjustAmount")
        strAdjustAmount = xmlTempNode.InnerText.Replace(",", "")
        xmlDOM.DocumentElement.RemoveChild(xmlTempNode)
        xmlTempNode = xmlDOM.DocumentElement.SelectSingleNode("ImportCostGUID")
        strImportCostGUID = xmlTempNode.InnerText
        xmlDOM.DocumentElement.RemoveChild(xmlTempNode)
        xmlTempNode = xmlDOM.DocumentElement.SelectSingleNode("ExportCostGUID")
        strExportCostGUID = xmlTempNode.InnerText
        xmlDOM.DocumentElement.RemoveChild(xmlTempNode)
        strKeyValue = xmlDOM.DocumentElement.GetAttribute("keyvalue")

        Dim oid As String = MyDB.SaveXml(xmlDOM.OuterXml)
        If strKeyValue = "" Then
            '新增
            strSql = "INSERT INTO cb_AdjustDtl(AdjustDtlGUID" & _
                                            ",AdjustGUID" & _
                                            ",CostGUID" & _
                                            ",IsSwitchTo" & _
                                            ",AdjustAmount" & _
                                    ") VALUES(newid()" & _
                                            ",'" & oid & "'" & _
                                            ",'" & strImportCostGUID & "'" & _
                                            ",1" & _
                                            "," & strAdjustAmount & _
                                    ")"
            strSql &= ";INSERT INTO cb_AdjustDtl(AdjustDtlGUID" & _
                                            ",AdjustGUID" & _
                                            ",CostGUID" & _
                                            ",IsSwitchTo" & _
                                            ",AdjustAmount" & _
                                    ") VALUES(newid()" & _
                                            ",'" & oid & "'" & _
                                            ",'" & strExportCostGUID & "'" & _
                                            ",0" & _
                                            ",-1*" & strAdjustAmount & _
                                    ")"
        Else
            '修改
            strSql = "UPDATE cb_AdjustDtl SET CostGUID='" & strImportCostGUID & "'" & _
                                            ",AdjustAmount=" & strAdjustAmount & _
                    " WHERE AdjustGUID='" & oid & "' AND IsSwitchTo=1"
            strSql &= ";UPDATE cb_AdjustDtl SET CostGUID='" & strExportCostGUID & "'" & _
                                            ",AdjustAmount=-1*" & strAdjustAmount & _
                    " WHERE AdjustGUID='" & oid & "' AND IsSwitchTo=0"
        End If
        If MyDB.ExecSQL(strSql) < 0 Then
            Return "<xml result=""false"" errormessage=""运行时异常，请与管理员联系！""></xml>"
        Else
            Return "<xml result=""true"" keyvalue=""" & oid & """></xml>"
        End If
    End Function

    '审核目标调整
    Function ApproveAdjust(ByVal AdjustGUID As String) As Boolean
        Dim strSql As String

        strSql = "Exec usp_cb_ApproveTargetAdjust '" & AdjustGUID & "'"

        If MyDB.ExecSQL(strSql) < 0 Then
            Return False
        Else
            Return True
        End If

    End Function

    '由预估变更GUID来更新cb_Cost表，先减去合同的预估变更
    Function CancelYGAlter(ByVal sContractGUID As String) As Boolean
        Dim strSql As String

        'strSql = "UPDATE cb_Cost Set YGAlter=YGAlter-A.SumYGAlter" & _
        '                            ",DtCost=DtCost-A.SumYGAlter,LayoutSpare=LayoutSpare+A.SumYGAlter" & _
        '        " FROM (select c.CostCode,c.ProjectCode,sum(a.CfAmount) as SumYGAlter" & _
        '                " from cb_YgAlter2Budget a" & _
        '                " left join cb_Budget b on a.BudgetGUID=b.BudgetGUID" & _
        '                " left join cb_Cost c on b.CostGUID=c.CostGUID" & _
        '                " where a.ContractGUID='" & sContractGUID & "' and c.CostCode Is Not Null" & _
        '                " group by c.ProjectCode,c.CostCode) A" & _
        '                " LEFT JOIN cb_Cost ON (A.CostCode = cb_Cost.CostCode or A.CostCode like cb_Cost.CostCode + '.%')  AND (A.ProjectCode = cb_Cost.ProjectCode or A.ProjectCode like cb_Cost.ProjectCode + '.%')"
        ''" LEFT JOIN cb_Cost ON Charindex(cb_Cost.CostCode+'.',A.CostCode+'.')=1  AND Charindex(cb_Cost.ProjectCode+'.',A.ProjectCode+'.')=1"

        strSql = "EXEC usp_cb_UpdateYgAlterCost '" & sContractGUID & "','CANCEL'"

        If MyDB.ExecSQL(strSql) < 0 Then
            Return False
        Else
            Return True
        End If
    End Function

    '由预估变更GUID来更新cb_Cost表 ,加上合同对应预估变更
    Function UseYGAlter(ByVal sContractGUID As String) As Boolean
        Dim strSql As String

        'strSql = "UPDATE cb_Cost Set YGAlter=YGAlter+A.SumYGAlter" & _
        '                            ",DtCost=DtCost+A.SumYGAlter,LayoutSpare=LayoutSpare-A.SumYGAlter" & _
        '        " FROM (select c.CostCode,c.ProjectCode,sum(a.CfAmount) as SumYGAlter" & _
        '                " from Cb_YgAlter2Budget a" & _
        '                " left join cb_Budget b on a.BudgetGUID=b.BudgetGUID" & _
        '                " left join cb_Cost c on b.CostGUID=c.CostGUID" & _
        '                " where a.ContractGUID='" & sContractGUID & "' and c.CostCode Is Not Null" & _
        '                " group by c.ProjectCode,c.CostCode) A" & _
        '                " LEFT JOIN cb_Cost ON (A.CostCode = cb_Cost.CostCode or A.CostCode like cb_Cost.CostCode + '.%')  AND (A.ProjectCode = cb_Cost.ProjectCode or A.ProjectCode like cb_Cost.ProjectCode + '.%')"
        ''" LEFT JOIN cb_Cost ON Charindex(cb_Cost.CostCode+'.',A.CostCode+'.')=1  AND Charindex(cb_Cost.ProjectCode+'.',A.ProjectCode+'.')=1"

        strSql = "EXEC usp_cb_UpdateYgAlterCost '" & sContractGUID & "','USE'"

        If MyDB.ExecSQL(strSql) < 0 Then
            Return False
        Else
            Return True
        End If
    End Function
    Function UpdateContractCfRule(ByVal sContractGUID As String) As Boolean
        '重新更新合同的拆分规则 aobo add on 20081210
        Dim strSql As String

        '1、更改合同拆分表的规则
        '2、更改cb_CfDtl表的拆分规则

        strSql = "EXEC usp_cb_UpdateContractCfRule '" & sContractGUID & "';Exec usp_cb_UpdateFactPayCostFt '" & sContractGUID & "', '合同', 0;"


        If MyDB.ExecSQL(strSql) < 0 Then
            Return False
        Else
            Return True
        End If
    End Function


    '功能：成本控制指标提醒
    '参数
    Function aa()

    End Function
End Class

'费项归属枚举
Public Enum ItemTypeEnum
    None = 0        '无定义
    Proj = 1        '项目
    Company = 2     '公司
    Dept = 3        '部门
End Enum

'费项类型枚举
Public Enum CostTypeEnum
    None = 0
    ProjCost = 1
    CompanyCost = 2
    DeptCost = 4
    GroupCost = 8
    AllCompanyCost = 16
End Enum